package com.geo.geotool.service.geofile;

import com.alibaba.fastjson.JSONArray;
import com.geo.geotool.entity.geo.GeoFileInfo;
import com.geo.geotool.service.geofile.fileCharset.GeoFileCharsetCaptureFactory;
import com.geo.geotool.service.geofile.fileCharset.IFileCharsetCapture;
import com.geo.geotool.util.file.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * • @Description: txt解析类
 * • @author  : yell
 * • @date : 2021/12/07  14:01
 */
public class TxtFileParser implements IGeoFileParser{
    private Map<String,String> zbxTransMap = new HashMap();
    {
        zbxTransMap.put("2000国家大地坐标系","4526");
    }
    private final String  GEOMETRY_TYPE = "Polygon";
    private final String  FEATURE_TYPE = "Feature";
    @Override
    public GeoFileInfo parserFile(String filePath) throws Exception {
        IFileCharsetCapture fileCharsetCapture =
                GeoFileCharsetCaptureFactory.getFileCharsetCapture(filePath);
        /**获取文件编码*/
        String charsetName = fileCharsetCapture.getCharsetName(filePath);

        String fileContent  = FileUtils.readFileToString(new File(filePath),charsetName);
        GeoFileInfo geoFileInfo = new GeoFileInfo();
        Map attributes = new HashMap<>();
        String[] projectInfos =
                fileContent.split("\\[属性描述\\]");
        proxyProjectInfo(projectInfos[0].replaceAll("\\[项目信息\\]",""),attributes);
        String[] secondSplit = projectInfos[1].split("\\[地块坐标\\]");
        parsesCoordinateInfo(secondSplit[0],attributes);
        geoFileInfo.setAttributes(attributes);
        geoFileInfo.setLayerName((String)attributes.get("xmmc"));
        String srId = zbxTransMap.get(attributes.get("zbx"));
        geoFileInfo.setSrId(srId);
        JSONArray jsonArray = getCoordinates(secondSplit[1],srId);
        geoFileInfo.setGeoJson(jsonArray);
        return geoFileInfo;
    }

    /***
     * 解析项目信息
     * @param fileContent
     * @return
     */
    public void proxyProjectInfo(String fileContent,Map<String,Object> attributes){
        String[] details = fileContent.split("\r\n");
        for(String proInfo : details){
            if (StringUtils.isBlank(proInfo) || !proInfo.contains("=") || proInfo.length() <=2) {
                continue;
            }
            String[] xx = proInfo.split("=");
            if(xx[0].equals("项目名称")){
                attributes.put("xmmc",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("项目所在县区代码")){
                attributes.put("xmszxqdm",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("项目所在市县名称")){
                attributes.put("xmszsxmc",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("项目类别")){
                attributes.put("xmlb",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("项目投资额")){
                attributes.put("xmtzz",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("开发用途")){
                attributes.put("kfyt",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("总用地面积")){
                attributes.put("zydmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("占用基本农田面积")){
                attributes.put("zyjbntmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("农用地面积")){
                attributes.put("nydmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("耕地面积")){
                attributes.put("gdmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("园地面积")){
                attributes.put("ydmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("林地面积")){
                attributes.put("ldmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("养殖水面面积")){
                attributes.put("yzsmmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("其他农用地面积")){
                attributes.put("qtnydmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("带K地类面积")){
                attributes.put("dkdlmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("建设用地面积")){
                attributes.put("jsydmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("未利用地面积")){
                attributes.put("wlydmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("围填海面积")){
                attributes.put("wthmj",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("是否增减挂钩项目")){
                attributes.put("sfzjggxm",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("是否属于增减挂钩中发展改革小城镇试点项目")){
                attributes.put("sfsyzjggzfzggxczsdxm",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("是否属于建设用地指标调整项目")){
                attributes.put("sfsyjsydzbtzxm",xx.length>1?xx.length>1?xx[1]:"":"");
            }else if(xx[0].equals("备注")){
                attributes.put("bz",xx.length>1?xx.length>1?xx[1]:"":"");
            }
        }
    }

    /**
     * 解析坐标系相关信息
     * @param fileContent
     * @return
     */
    public void parsesCoordinateInfo(String fileContent,Map<String,Object> attributes){
        String[] details = fileContent.split("\r\n");
        for(String attInfo : details){
            if (StringUtils.isBlank(attInfo) || !attInfo.contains("=") || attInfo.length() <=2) {
                continue;
            }
            String[] xx = attInfo.split("=");
            if(xx[0].equals("坐标系")){
                attributes.put("zbx",xx.length>1?xx[1]:"");
            }else if(xx[0].equals("几度分带")){
                attributes.put("jdfd",xx.length>1?xx[1]:"");
            }else if(xx[0].equals("投影类型")){
                attributes.put("tylx",xx.length>1?xx[1]:"");
            }else if(xx[0].equals("计量单位")){
                attributes.put("jldw",xx.length>1?xx[1]:"");
            }else if(xx[0].equals("带号")){
                attributes.put("dh",xx.length>1?xx[1]:"");
            }else if(xx[0].equals("精度")){
                attributes.put("jd",xx.length>1?xx[1]:"");
            }else if(xx[0].equals("转换参数")){
                attributes.put("zhcs",xx.length>1?xx[1]:"");
            }
        }
    }

    public JSONArray getCoordinates(String fileContent,String siId){
        JSONArray jsonArray = new JSONArray();
        String[] details = fileContent.split("\r\n");
        List<List<double[]>> rings = null;
        Map<String,Object> landDesc = null;
        /**圈号*/
        String qh = "";
        for(String coordinateInfo : details){
            if(StringUtils.isBlank(coordinateInfo)){
                continue;
            }
            if(coordinateInfo.contains("@")){
                if(rings != null){
                    /**构建上一块的数据**/
                    consteGeoJSON(jsonArray, rings, landDesc,siId);
                    qh = "";
                }
                /**构建新地块信息**/
                rings = new ArrayList<>();
                /**增加总体描述**/
                String[] landPieceDesc = coordinateInfo.split(",");
                String dkmj = landPieceDesc[1].replaceAll("[\\t\\n\\r]","");
                String dkmc = landPieceDesc[3].replaceAll("[\\t\\n\\r]","");
                if(StringUtils.isBlank(dkmc)){
                    dkmc = "地块"+(rings.size()==0?1:rings.size()+1);
                }
                String dkbh = landPieceDesc[2].replaceAll("[\\t\\n\\r]","");
                String dkyt = landPieceDesc[6].replaceAll("[\\t\\n\\r]","");
                String tfh = landPieceDesc[5].replaceAll("[\\t\\n\\r]","");
                String jzds = landPieceDesc[0].replaceAll("[\\t\\n\\r]","");
                landDesc = new HashMap<>();
                landDesc.put("dkmj",dkmj);
                landDesc.put("dkmc",dkmc);
                landDesc.put("dkbh",dkbh);
                landDesc.put("dkyt",dkyt);
                landDesc.put("tfh",tfh);
                landDesc.put("jzds",jzds);
            }else {
                String[] coor = coordinateInfo.split(",");
                String coordinate_index = coor[1];
                if(rings.size() == 0 || !qh.equals(coordinate_index)){
                    qh = coordinate_index;
                    rings.add(new ArrayList<>());
                }
                /**获取最新的坐标集合**/
                List<double[]> ring = rings.get(rings.size() - 1);
                Double coordinate_first = Double.valueOf(coor[2]);
                Double coordinate_second = Double.valueOf(coor[3]);
                double[] coordinate = {coordinate_second,coordinate_first};
                ring.add(coordinate);
            }
        }
        consteGeoJSON(jsonArray, rings, landDesc,siId);
        return jsonArray;
    }

    private void consteGeoJSON(JSONArray jsonArray, List<List<double[]>> rings, Map<String, Object> landDesc,String srId) {
        Map<String,Object> geometryMap = new HashMap<>(2);
        geometryMap.put("coordinates",rings);
        geometryMap.put("type",GEOMETRY_TYPE);
        geometryMap.put("crs","EPSG:"+srId);
        Map<String,Object> featureMap = new HashMap<>(1);
        featureMap.put("geometry",geometryMap);
        featureMap.put("type",FEATURE_TYPE);
        featureMap.put("properties",landDesc);
        jsonArray.add(featureMap);
    }
}
